﻿using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Client;
using RedDragon.RBAC.ErrorCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;
namespace RedDragon.RBAC.API.Application.Common.Sale
{
    public class DeleteSaleHandler : IRequestHandler<DeleteSaleCommand, Result>
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 权限仓储
        /// </summary>
        public readonly IBaseRepository<Sales> SalesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DeleteSaleHandler> logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        /// <param name="SalesRepository"></param>
        /// <param name="logger"></param>
        public DeleteSaleHandler(ISqlSugarClient db, IBaseRepository<Sales> SalesRepository, ILogger<DeleteSaleHandler> logger)
        {
            this.db = db;
            this.SalesRepository = SalesRepository;
         
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DeleteSaleCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = await (await SalesRepository.GetAll()).Where(d => d.SalesId == request.SalesId).FirstAsync();
                if (list != null)
                {
                    list.IsDel = 1;
                }
                var res = SalesRepository.Update(list);
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "订单删除成功";
            }
            catch (Exception ex)
            {
                logger.LogError("订单删除异常");
                result.Msg = "订单删除异常";
            }
            return result;
        }
    }
}
